package it.duecaffe.claysoftware.interventi.client.view;

import it.duecaffe.claysoftware.interventi.client.handler.MenuEvent;
import it.duecaffe.claysoftware.interventi.client.handler.Utils;
import it.duecaffe.claysoftware.interventi.client.images.ClayResources;
import it.duecaffe.claysoftware.interventi.shared.models.MenuModel;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardPagingPolicy.KeyboardPagingPolicy;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class MenuPanel extends VerticalPanel {

	private ListDataProvider<MenuModel> dataProvider = new ListDataProvider<MenuModel>();
	private ClayResources IMAGES = GWT.create(ClayResources.class);
	
	  /**
	   * The Cell used to render a {@link MenuModel}.
	   */
	  static class MenuCell extends AbstractCell<MenuModel> {

	    @Override
	    public void render(Context context, MenuModel value, SafeHtmlBuilder sb) {
	      // Value can be null, so do a null check..
	      if (value == null) {
	        return;
	      }

	      sb.appendHtmlConstant("<table>");

	      // Add the contact image.
	      sb.appendHtmlConstant("<tr><td rowspan='2'>");
	      sb.appendHtmlConstant(AbstractImagePrototype.create(value.getIcona()).getHTML());
	      sb.appendHtmlConstant("</td>");

	      // Add the name and address.
	      sb.appendHtmlConstant("<td style='font-size:95%;'>");
	      sb.appendEscaped(value.getDescrizione());
//	      sb.appendHtmlConstant("</td></tr><tr><td>");
//	      sb.appendEscaped(value.getAddress());
	      sb.appendHtmlConstant("</td></tr></table>");
	    }
	  }
	    
	public MenuPanel() {
		setWidth("100%");
		setHeight("100%");
		setBorderWidth(2);
		init();
	}
	
	private void init() {
		
		createMenu();
		
		 // Create a CellList.
	    MenuCell contactCell = new MenuCell();

	    CellList<MenuModel> cellList = new CellList<MenuModel>(contactCell);
	    cellList.setPageSize(30);
	    cellList.setKeyboardPagingPolicy(KeyboardPagingPolicy.INCREASE_RANGE);
	    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION);

	    // Add a selection model so we can select cells.
	    final SingleSelectionModel<MenuModel> selectionModel = new SingleSelectionModel<MenuModel>();
	    cellList.setSelectionModel(selectionModel);
	    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
	      public void onSelectionChange(SelectionChangeEvent event) {
	    	  Utils.EVENT_BUS.fireEvent(new MenuEvent(selectionModel.getSelectedObject()));
	      }
	    });
	    
	    dataProvider.addDataDisplay(cellList);
	    
	    add(cellList);

	}
	
	private void createMenu() {
		// Menu nuovo intervento
		MenuModel menuNuovoInt = new MenuModel();
		menuNuovoInt.setIdMenu("NEW");
		menuNuovoInt.setDescrizione("Nuovo intervento");
		menuNuovoInt.setIcona(IMAGES.add_16());
		dataProvider.getList().add(menuNuovoInt);
		
		// Menu ricerca interventi
		MenuModel menuRicInt = new MenuModel();
		menuRicInt.setIdMenu("RIC");
		menuRicInt.setDescrizione("Ricerca intervento");
		menuRicInt.setIcona(IMAGES.add_16());
		dataProvider.getList().add(menuRicInt);
	}
}
